﻿#region DaProductCheckAbility
/*----------------------------------------------------------------
// 文件名：DaProductCheckAbility.cs
// 功能描述：表dbo.Product_Check_Ability的数据访问层
//
// 
// 创建时间：2009-11-22 DtataAccess template . Ver 5.0.20090413
//
// 修改标识： 
// 修改描述： 
//----------------------------------------------------------------*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Globalization;
using System.Text;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

using EQT.Model;


namespace EQT.Dal
{
    ///可以继承更上层接口,IProductCheckAbility ,数据访问层基类DaBase，
    ///DaBase中包含了DbException异常处理静态方法
    public partial class DaProductCheckAbility : DaBase<MoProductCheckAbility>
    {
        #region SQL const
        internal const string SQL_GET_DISTINCT_PRODUCT = "SELECT DISTINCT PNAME FROM PRODUCT_CHECK_ABILITY WHERE PCA_STATE=0";
        internal const string SQL_GET_DISTINCT_MODEL = "SELECT DISTINCT PMODEL FROM PRODUCT_CHECK_ABILITY WHERE PNAME='{0}'AND PCA_STATE=0";
        internal const string SQL_GET_TYPE = "SELECT * FROM V_PRODUCT_CHECK_ABILITY  WHERE PNAME='{0}' AND PMODEL='{1}'";
        internal const string SQL_DELETE_FILE = "UPDATE PRODUCT_CHECK_ABILITY  SET {0}='' WHERE PCA_ID='{1}'";
        #endregion

        /// <summary>
        /// 获取所有产品。
        /// </summary>
        /// <returns></returns>
        public DataSet GetDistinctProduct()
        {
            try
            {
                DbCommand dbCommand = db.GetSqlStringCommand(SQL_GET_DISTINCT_PRODUCT);

                return db.ExecuteDataSet(dbCommand);
            }
            catch (DbException ex)
            {
                HandleDbException(ex);
                return null;
            }
        }

        /// <summary>
        /// 获取某产品下的所有型号。
        /// </summary>
        /// <param name="strProductName">产品名称</param>
        /// <returns></returns>
        public DataSet GetDistinctModel(string strProductName)
        {
            try
            {
                string strSql = string.Format(SQL_GET_DISTINCT_MODEL, strProductName);

                DbCommand dbCommand = db.GetSqlStringCommand(strSql);

                return db.ExecuteDataSet(dbCommand);
            }
            catch (DbException ex)
            {
                HandleDbException(ex);
                return null;
            }
        }

        /// <summary>
        /// 获取某产品和型号下所有的参数名称。
        /// </summary>
        /// <param name="pname">产品名</param>
        /// <param name="pmodel">型号名</param>
        /// <returns></returns>
        public DataSet GetProductType(string pname,string pmodel)
        {
            try
            {
                string strSql = string.Format(SQL_GET_TYPE, pname,pmodel);

                DbCommand dbCommand = db.GetSqlStringCommand(strSql);

                return db.ExecuteDataSet(dbCommand);
            }
            catch (DbException ex)
            {
                HandleDbException(ex);
                return null;
            }
        }

        /// <summary>
        /// 删除产品检验项模板文件。
        /// </summary>
        /// <param name="pid">检验项ID</param>
        /// <param name="flag">字段标示</param>
        /// <returns></returns>
        public bool RemoveTemplateFile(string spid, int flag)
        {
            string[] pid=spid.Split(',');//0 pca_id,1 file_id;

            string strSql = string.Empty;

            switch (flag)
            { 
                case 1:
                    strSql = string.Format(SQL_DELETE_FILE, "Tlt_Xls_PType", pid[0]);
                    break;
                case 2:
                    strSql = string.Format(SQL_DELETE_FILE, "Tlt_Doc_Report", pid[0]);
                    break;
                case 3:
                    strSql = string.Format(SQL_DELETE_FILE, "Tlt_Xls_DDL", pid[0]);
                    break;
                case 4:
                    strSql = string.Format(SQL_DELETE_FILE, "Tlt_Doc_DDL", pid[0]);
                    break;
                case 5:
                    strSql = string.Format(SQL_DELETE_FILE, "Tlt_Xls_DRL", pid[0]);
                    break;
                case 6:
                    strSql = string.Format(SQL_DELETE_FILE, "Tlt_Doc_DRL", pid[0]);
                    break;
                default:
                    return false;
            }

            
                DaRefFile dalRefFile = new DaRefFile(db);
                DaTfiles dalFiles = new DaTfiles(db);

                using (DbConnection conn = db.CreateConnection())
                {
                    try
                    {
                        int rc = 0;
                        conn.Open();
                        using (DbTransaction trans = conn.BeginTransaction())
                        {
                            IList<MoRefFile> list = dalRefFile.GetEntities(" where EID='" + pid[0] + "' and file_id='"+pid[1]+"'");

                            foreach (MoRefFile mod in list)
                            {
                                rc += dalRefFile.DeleteByWhereClause(String.Format(" WHERE RF_ID='{0}' and eid='{1}'", mod.RfId,pid[0]), trans);
                                rc += dalFiles.DeleteByWhereClause(String.Format(" WHERE FILE_ID='{0}'", mod.FileId), trans);
                            }
                            DbCommand dbCommand = db.GetSqlStringCommand(strSql);
                            db.ExecuteNonQuery(dbCommand) ;
                            trans.Commit();
                        }
                        conn.Close();
                        return true;
                    }
                    catch (DbException de)
                    {
                        throw de;
                    }
                }
            
        }
    }
}
#endregion

